local util = require "util"
local conf = require "conf"
local require = require
local setmetatable = setmetatable
local error = error
local table = table
local ngx = ngx
local os = os
local string = string
local tonumber = tonumber
local pairs = pairs
package.cpath = "/usr/nginx/?.so;;"
local json = require 'cjson'
module(...)
_VERSION = '0.1'

function create(user,args)
	local db = conf.get_mysql()
	local sql = "INSERT INTO store_item SET "

	if args.id then
		sql = sql .. "id = " .. args.id .. ","
	end
	if args.store_id then
		sql = sql .. "store_id = " .. args.store_id .. ","
	end

	if args.item_id then
		sql = sql .. "item_id = " .. args.item_id .. ","
	end

	if args.amount then
		sql = sql .. "amount = " .. args.amount .. ","
	end

	if args.price then
		sql = sql .. "price = " .. args.price .. ","
	end

	if args.sort then
		sql = sql .. "sort = " .. args.sort .. ","
	end

	if args.start_time then
		sql = sql .. "start_time = " .. args.start_time .. ","
	end

	if args.end_time then
		sql = sql .. "end_time = " .. args.end_time .. ","
	end

	if args.disabled then
		sql = sql .. "disabled = " .. args.disabled .. ","
	end

	sql = sql .. "create_time = " .. os.time() .. ","
    local res = db:query(sql:sub(1,-2))
    ngx.log(ngx.ERR, json.encode(res))
    return res
end

function createController(user,args)
    local db = conf.get_mysql()

    local res = create(user,args)
    if not res or res.affected_rows < 1 then
        ngx.print(json.encode({
            err = 1,
            msg = '创建失败'
        }))
        ngx.exit(200)
    else
        local insert_id = res.insert_id
        if insert_id < 1 then
            res = db:query("SELECT LAST_INSERT_ID() AS insert_id")
            if res and res[1] and res[1].insert_id then
                insert_id = res[1].insert_id
            end
        end
        ngx.print(json.encode({
            err = 0,
            id = insert_id,
            msg = '创建成功'
        }))
        ngx.exit(200)
    end 
end

function update(user,args)
	local db = conf.get_mysql()
	local sql = "UPDATE store_item SET "

	if args.id then
		sql = sql .. "id = " .. args.id .. ","
	end

	if args.store_id then
		sql = sql .. "store_id = " .. args.store_id .. ","
	end

	if args.item_id then
		sql = sql .. "item_id = " .. args.item_id .. ","
	end

	if args.amount then
		sql = sql .. "amount = " .. args.amount .. ","
	end

	if args.price then
		sql = sql .. "price = " .. args.price .. ","
	end

	if args.sort then
		sql = sql .. "sort = " .. args.sort .. ","
	end

	if args.start_time then
		sql = sql .. "start_time = " .. args.start_time .. ","
	end

	if args.end_time then
		sql = sql .. "end_time = " .. args.end_time .. ","
	end

	if args.disabled then
		sql = sql .. "disabled = " .. args.disabled .. ","
	end

    sql = sql .. " update_time = " .. os.time() .. ","
    sql = sql:sub(1,-2) .." WHERE id = " .. args.id
    local res = db:query(sql)
    ngx.log(ngx.ERR, sql)
    return res
end

function updateController(user,args)
    if not args.id then
        ngx.print(json.encode({
            err = 2,
            msg = '缺少id'
        }))
    end
    local res = update(user,args)
    if not res  then
        ngx.print(json.encode({
            err = 1,
            msg = '更新失败'
        }))
        ngx.exit(200)
    else 
        ngx.print(json.encode({
            err = 0,
            msg = res
        }))
        ngx.exit(200)
    end
end

function delete(user,args)
	local id = args.id
	local db = conf.get_mysql()
	local res = db:query("DELETE FROM store_item where id = " .. id)
	return res
end

function deleteController(user,args)
	if not args.id then
		ngx.print(json.encode({
            err = 2,
            msg = '缺少id'
        }))
	end
	local res = delete(user,args)
	if not res or res.affected_rows < 1 then 
		ngx.print(json.encode({
			err = 1,
			msg = '更新失败'
			}))
		ngx.exit(200)
	else
		ngx.print(json.encode({
			err = 0,
			msg = res
			}))
		ngx.exit(200)
	end
end

function search(user,args)

	local db = conf.get_mysql()
	local now = os.time()
	--local sql = "SELECT * FROM 'plane',store_item WHERE plane.id=store_item.store_id";
	local sql = "SELECT item.id as item_id ,item.name,item.price,item.weight,item.length,item.width,item.height,item.icon_id,item.category_id,item.disabled,store_item.id as store_item_id ,store_item.store_id,store_item.amount,store_item.price,store_item.sort,store_item.disabled " ..
				"FROM store_item,item where store_item.item_id = item.id "

	if args.store_id then 
		sql = sql .. " AND store_item.store_id = " .. args.store_id
	end

	if args.id then
		sql = sql .. " AND store_item.id = " .. args.id
	end

	if args.name then
		sql = sql .." AND store_item.name = " .. args.name
	end

	ngx.log(ngx.ERR, sql)
	if db:query(sql) then 
		local count = #(db:query(sql))
	else
		local count = 0
	end

	local res = db:query(sql)
	ngx.log(ngx.ERR, json.encode(res))
	return res,count
end


local function searchController(user,args)
	local res ,count = search(user,args)
	if not res then 
		ngx.print(json.encode({
			err = 1,
			mse = ' ' 
			}))
		ngx.exit(200)
	else
		ngx.print(json.encode({
			err = 0,
			msg = res,
			count = count
			}))
		ngx.exit(200)
	end
end

function default(args)
    local user = conf.parse_token(args, "spoint")
    if args.action == 'create' then
        createController(user,args)
    elseif args.action == 'update' then
        updateController(user,args)
    elseif args.action == 'search' then
        searchController(user,args)
    elseif args.action == 'delete' then
        deleteController(user,args)
    elseif args.action == 'list' then
        listController(user,args)
    end
end

local class_mt = {
    __newindex = function (table, key, val)
        error('attempt to write to undeclared variable "' .. key .. '"')
    end
}
setmetatable(_M, class_mt)

